package com.wujunshen.tdd.flags;

import com.wujunshen.tdd.common.FlagType;
import com.wujunshen.tdd.model.Parameter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.wujunshen.tdd.common.Constants.COMMA;
import static com.wujunshen.tdd.common.FlagType.STRING_ARRAY;

/**
 * @author frank woo(吴峻申) <br>
 *     email:<a href="mailto:frank_wjs@hotmail.com">frank_wjs@hotmail.com</a> <br>
 * @date 2021/12/15 14:01<br>
 */
public class StringArrayFlag implements Flag<String[]> {
  public static final List<Parameter<String[]>> STRING_ARRAY_PARAMETERS = new ArrayList<>();

  @Override
  public void initParameters(String flag, FlagType type) {
    if (type == STRING_ARRAY) {
      STRING_ARRAY_PARAMETERS.add(new Parameter<>(flag, STRING_ARRAY, new String[] {}));
    }
  }

  @Override
  public void setValue(String flag, Iterator<String> argIter) {
    for (Parameter<String[]> parameter : STRING_ARRAY_PARAMETERS) {
      if (flag.equals(parameter.getFlag())) {
        parameter.setValue(argIter.next().trim().split(COMMA));
      }
    }
  }

  @Override
  public String[] getValue(String flag) {
    for (Parameter<String[]> parameter : STRING_ARRAY_PARAMETERS) {
      if (flag.equals(parameter.getFlag())) {
        return parameter.getValue();
      }
    }
    throw new IllegalArgumentException("flag type mismatch: " + flag);
  }
}
